package rickyGame.game.action
{
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.game.graphic.Graphic;
	import ricky.utils.FrameManager;
	import ricky.utils.TimeServer;
	
	import ricky.game.Actor2D;
	import rickyGame.game.SkillObject;
	import rickyGame.game.skill.SkillData;
	import rickyGame.net.MsgType;
	import rickyGame.net.WorkQueue;
	
	/**
	 * 追踪命中目标
	 */
	public class FollowHitAct extends Act2D
	{
		public function FollowHitAct(actor:Actor,target:Actor,skill:SkillData)
		{
			super(actor);

			if(actor is SkillObject)
				this._operator=SkillObject(actor).actor;
			this._target=target;
			this._skill=skill;
			_calcPoint=new Vector3D(_target.graphic.x,_target.graphic.y);
			_coord=new Vector3D();
		}
		
		override public function begin():void
		{
//			JDebug.trace("bollowHit行动begin"+actor);
			this._state=1;
			_begin=TimeServer.now();
			_lastTime=_begin;
			FrameManager.inst.addLogic(getActor());
		}
		
		override public function action(timeline:Number):void
		{
			var dot:Number=timeline-_lastTime;
			_lastTime=timeline;
			var faraway:Number=(timeline-_begin)/1000;
			if(faraway>10)
			{
				this.complete();
				return;
			}
			
			_calcPoint.x=_target.graphic.x;
			_calcPoint.y=_target.graphic.y;

			_coord.x=actor.graphic.x;
			_coord.y=actor.graphic.y;
			
			var actor2d:Actor2D=actor as Actor2D;
			
			var desiredvelocity:Vector3D =_calcPoint.subtract(_coord);
			desiredvelocity.normalize();//actor.speed/40
//			desiredvelocity.scaleBy(
			var force:Vector3D = desiredvelocity.subtract(actor.graphic.velocityPu);
			var myVelocity:Vector3D=actor.graphic.velocityPu;
			actor.graphic.velocityPu= myVelocity.add(force);
			_coord=_coord.add(actor.graphic.velocityPu);
			
			//计算方向
			var dx:Number=actor.graphic.velocityPu.x;
			var dy:Number=actor.graphic.velocityPu.y;
			var angle:Number= Math.atan2(dy,dx);//角度

			actor.graphic.x=_coord.x;
			actor.graphic.y=_coord.y;

			Graphic(actor.graphic).clothes.rotation=angle*Common.radianToAngle;////57.29577884903978;//180/Math.PI
			
			if(isArrive())
			{
//				WorkQueue.instance.pushRecive(MsgType.SERVER_SKILL_EFFECT_TARGETS,_operator,[_target],this._skill);
				this.complete();
			}
		}
		
		override public function end():void
		{
//			JDebug.trace("FollowHit行动end"+actor);
			Graphic(_actor.graphic).clothes.rotation=0;
			FrameManager.inst.removeLogic(getActor());
			super.end();
		}
		
		/**
		 * 是否移动到目标x,y
		 */
		protected function isArrive():Boolean
		{
			_calcPoint.x=_target.graphic.x;
			_calcPoint.y=_target.graphic.y;
			
			_coord.x=this.actor.graphic.x;
			_coord.y=this.actor.graphic.y;
			
			if(_coord.subtract(_calcPoint).length<_radius)//if(x==this._currentObject.coordinate.x && this._currentObject.coordinate.y==y)
				return true;
			else
				return false;
		}
		
		private var _calcPoint:Vector3D;//计算用的点
		private var _coord:Vector3D;//自身的坐标
		private var _radius:Number=10;//到达半径
		
		private var _operator:Actor;//操作者
		private var _target:Actor;//目标方向
		private var _skill:SkillData;//技能数据
		
		private var _lastTime:Number;//
		private var _begin:Number;
		private var _vx:Number=0;
		private var _vy:Number=0;
	}
}